// 新路径
import { BaseDTO } from '#src/common/dto/base.dto.js';
import { ValidationError } from '#src/common/errors/index.js';
import Joi from 'joi';

/**
 * @file request/user.js
 * @description 用户相关的请求DTO
 */

/**
 * 用户注册请求DTO
 * @typedef {Object} UserRegisterDTO
 * @property {string} username - 用户名
 * @property {string} email - 邮箱
 * @property {string} password - 密码
 */
export class UserRegisterDTO extends BaseDTO {
    /** @type {Joi.Schema} */
    static schema = Joi.object({
        username: Joi.string().min(2).max(50).required()
            .messages({
                'string.pattern.base': '用户名只能包含中文、字母和数字'
            }),
        email: Joi.string()
            .email()
            .max(100)
            .required(),
        password: Joi.string()
            .min(8).max(30)
            .pattern(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]+$/)
            .required()
            .messages({
                'string.pattern.base': '密码必须包含大小写字母、数字和特殊字符'
            })
    });

    /**
     * @param {Object} data
     * @param {string} data.username
     * @param {string} data.email
     * @param {string} data.password
     */
    constructor(data) {
        super();
        const validated = this.constructor.validate(this.constructor.schema, data);
        Object.assign(this, validated);
    }

    /**
     * 从请求创建DTO
     * @param {Object} req - Express请求对象
     * @returns {UserRegisterDTO}
     */
    static fromRequest(req) {
        if (!req?.body) {
            throw new ValidationError('无效的请求数据');
        }
        return new UserRegisterDTO(req.body);
    }
}

/**
 * 用户登录DTO
 * @extends BaseDTO
 */
export class UserLoginDTO extends BaseDTO {
    /** @type {Joi.Schema} */
    static schema = Joi.object({
        email: Joi.string()
            .email()
            .required()
            .messages({
                'string.email': '邮箱格式不正确',
                'any.required': '邮箱不能为空'
            }),
        password: Joi.string()
            .required()
            .messages({
                'any.required': '密码不能为空'
            })
    });

    /**
     * @param {Object} data
     * @param {string} data.email
     * @param {string} data.password
     */
    constructor(data) {
        super();
        const validated = this.constructor.validate(this.constructor.schema, data);
        Object.assign(this, validated);
    }

    static fromRequest(req) {
        if (!req?.body) {
            throw new ValidationError('无效的请求数据');
        }
        return new UserLoginDTO(req.body);
    }
}

/**
 * 用户更新个人信息请求DTO
 * @typedef {Object} UserUpdateDTO
 * @property {string} [username] - 用户名
 * @property {string} [email] - 邮箱
 */
export class UserUpdateDTO extends BaseDTO {
   static schema = Joi.object({
    username: Joi.string().min(2).max(50),
    email: Joi.string().email().max(100)
}).min(1); // 至少需要一个字段

    constructor(data) {
        super();
        const validated = this.constructor.validate(this.constructor.schema, data);
        Object.assign(this, validated);
    }

    static fromRequest(req) {
        if (!req?.body) {
            throw new ValidationError('无效的请求数据');
        }
        return new UserUpdateDTO(req.body);
    }
}